﻿@page "/new-admin"
@attribute [Authorize]
@inject IAccessTokenProvider TokenProvider
@inject NavigationManager Navigation

@if (_error == true)
{
    <p>Could not get the access token.</p>
}
else if (_error == false)
{
    <p>Successfully added to the admin group.</p>
}

@code {

    private bool? _error;

    protected override async Task OnInitializedAsync()
    {
        var tokenResult = await TokenProvider.RequestAccessToken();

        if (tokenResult.TryGetToken(out var token))
        {
            var client = new HttpClient() { BaseAddress = new Uri(Navigation.BaseUri) };
            var request = new HttpRequestMessage(HttpMethod.Post, "Roles/MakeAdmin");
            request.Headers.Add("Authorization", $"Bearer {token.Value}");
            var response = await client.SendAsync(request);
            response.EnsureSuccessStatusCode();

        }
        else
        {
            _error = true;
        }
    }
}
